package com.aisier.architecture.base

import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.View
import androidx.annotation.LayoutRes
import androidx.fragment.app.Fragment
import com.zzq.my.jetpacks.util.LoadingDialogs

/*
  * ================================================
  * 描述：抽象类BaseFragment
  * 作者：zhuangzeqin
  * 时间: 2021/11/5-16:14
  * 邮箱：zzq@eeepay.cn
  * 备注:Androidx 下支持栏加载的fragment
  * ----------------------------------------------------------------
  * You never know what you can do until you try !
  *      _              _           _     _   ____  _             _ _
  *     / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
  *    / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
  *   / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
  *  /_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
  *
  * 签名：最痛苦的事不是我失败了，而是我本可以.--zzq
  * ----------------------------------------------------------------
  * ================================================
  */
abstract class BaseFragment(@LayoutRes contentLayoutId: Int) : Fragment(contentLayoutId),IUiView {
    protected val TAG = javaClass.simpleName
    protected lateinit var mActivity: Activity
    protected var mBundle: Bundle? = null
    /* ------注释说明---加载弹层----- */
    private var dialog: Dialog? = null
    /**
     * 是否执行懒加载
     */
    private var isLoaded = false
    override fun onAttach(context: Context) {
        super.onAttach(context)
        mActivity = context as Activity
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        if (arguments != null) {
            mBundle = arguments //参数Bunlder
        }
        super.onCreate(savedInstanceState)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initConfig()
        initView()
        initData()
    }

    /**
     * 显示加载弹框
     * cancelable
     */
    override fun showLoading(msg: String, cancelable: Boolean) {
        if (!::mActivity.isInitialized) return//未初始化
        if (!mActivity.isFinishing && !isHidden) {
            if (dialog == null) {
                dialog = LoadingDialogs.createLoadingDialog(mActivity,msg)
                with(dialog!!) {
                    //点击空白取消
                    setCanceledOnTouchOutside(cancelable)
                    setCancelable(cancelable)
                }
            }
            dialog?.show()
        }
    }

    /**
     * 关闭弹层
     */
    override fun dismissLoading() {
        if (!::mActivity.isInitialized) return//未初始化
        dialog?.takeIf { it.isShowing }?.dismiss()
    }

    override fun onResume() {
        super.onResume()
        if (!isLoaded && !isHidden) {
            lazyInit()//执行懒加载
            isLoaded = true
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        isLoaded = false
    }
    open fun initConfig() {
    }

    open fun initView() {

    }

    open fun initData() {

    }

    //懒加载初始化
    abstract fun lazyInit()
}